package utils;

import java.util.ResourceBundle;
import javax.faces.application.FacesMessage;
import javax.faces.application.FacesMessage.Severity;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author ZimmE
 */
public class JSFUtils {

    /**
     * Gets current
     * <code>FacesContext</code>
     *
     * @return FacesContext
     */
    public static FacesContext getFacesContext() {
        return FacesContext.getCurrentInstance();
    }

    /**
     * Gets current
     * <code>HttpServletRequest</code>
     *
     * @return HttpServletRequest
     */
    public static HttpServletRequest getRequest() {
        return (HttpServletRequest) getFacesContext().getExternalContext().getRequest();
    }

    /**
     * Adds a message to current
     * <code>FacesContext</code>
     */
    public static void addMessage(Severity severity, String message) {
        getFacesContext().addMessage(null, new FacesMessage(severity, message, null));
    }

    /**
     * Gets the localized message from
     * <code>ResourceBundle</code>
     *
     * @param label
     * @return String
     */
    public static String getLocalizedMessage(String label) {
        ResourceBundle messages = ResourceBundle.getBundle("utils.messages", getFacesContext().getViewRoot().getLocale());
        return messages.getString(label);
    }

    public static String getCurrentUsername() {
        return getRequest().getUserPrincipal().getName();
    }
}
